//
//  QGCommonHeadView.m
//  qeegooChat
//
//  Created by heew on 16/1/7.
//  Copyright © 2016年 com.autozi. All rights reserved.
//

#import "QGCommonHeadView.h"
#import "QGAsyncImageView.h"
@interface QGCommonHeadView ()

@property (nonatomic,strong)QGAsyncImageView *contentImageView;
@end
@implementation QGCommonHeadView

- (instancetype)init
{
    if (self = [super init]) {
        
        [self initSubViews];
    }
    
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self initSubViews];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.contentImageView.frame = self.bounds;
    self.contentImageView.layer.cornerRadius = self.bounds.size.width/2;
    self.contentImageView.layer.masksToBounds = YES;
}

- (void)initSubViews
{
    self.backgroundColor = [UIColor clearColor];
    self.contentImageView = [[QGAsyncImageView alloc]initWithFrame:self.bounds];
    self.contentImageView.image = [UIImage imageNamed:@"好友头像占位-bg-列表"];
    self.contentImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.contentImageView.clipsToBounds = YES;
    
    QGWeakObject(self) weakSelf = self;
    [self.contentImageView configDownloadTaskCompletionBlock:^(QGAsyncImageView *imageView, BOOL completion) {
        if (completion) {
            [weakSelf completionTask:completion withImage:imageView.image];
        }
    }];
    [self addSubview:self.contentImageView];
}

- (void)setHeadUrl:(NSString *)url {
    [self setHeadUrl:url headViewType:GJGCCommonHeadViewTypeContact];
}

- (void)setHeadUrl:(NSString *)url headViewType:(GJGCCommonHeadViewType)headViewType
{
    switch (headViewType) {
        case GJGCCommonHeadViewTypePGGroup: {
            [self.contentImageView setImage:[UIImage imageNamed:@"群组头像占位-bg-3行列表"]];
            break;
        }
        case GJGCCommonHeadViewTypeContact: {
            [self.contentImageView setImage:[UIImage imageNamed:@"好友头像占位-bg-列表"]];
            break;
        }
        case GJGCCommonHeadViewTypePostContact: {
            [self.contentImageView setImage:[UIImage imageNamed:@"IM头像"]];
            break;
        }
        default:
            break;
    }
    
    [self.contentImageView setUrl:url];
}

- (void)setHeadImage:(UIImage *)image
{
    [self.contentImageView setImage:image];
}

- (void)setHiddenImageView:(BOOL)hidden
{
    self.contentImageView.hidden = hidden;
}

- (void)completionTask:(BOOL)completion  withImage:(UIImage *)image
{
    self.contentImageView.image = image;
}

@end
